<template>
  <el-container class="home-container home">
    <!-- 头部区域 -->
    <el-header>
      <div class="header-box">
        <img src="../assets/3.jpeg" alt="">
        <span>图书后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 页面主题区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="toggelMenu ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollpase">|||</div>
        <!-- 侧边栏菜单区域 -->
        <el-menu
        background-color="#313743"
        text-color="#fff"
        active-text-color="#429af7"
        unique-opened
        :collapse="toggelMenu"
        :collapse-transition="false"
        :router="true"
        :default-active="defaultPath"
        >
          <!-- 一级菜单 -->
          <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
            <!-- 一级菜单的模板区 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconsObj[item.id]"></i>
              <!-- 文本 -->
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item @click="saveNavState('/'+ child.path)" :index="'/'+ child.path" v-for="child in item.children" :key="child.id">
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{child.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主题区域 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'Home',
  data () {
    return {
      menuList: [],
      iconsObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      // 菜单折叠
      toggelMenu: false,
      // 高亮显示
      defaultPath: ''
    }
  },
  created () {
    this.getMenulist()
    // console.log(this.iconsObj[125])
    this.defaultPath = JSON.parse(window.sessionStorage.getItem('activePath'))
  },
  methods: {
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取首页菜单栏数据
    // 简化promise操作使用 async
    async getMenulist () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menuList = res.data
    },
    // 切换菜单的折叠和展开
    toggleCollpase () {
      this.toggelMenu = !this.toggelMenu
    },
    // 点击侧面菜单，将当前得值赋值给sessoin，然后再取出来
    saveNavState (activePath) {
      // 设置当前的 激活 目录
      this.defaultPath = activePath
      // 存在webStorage里面
      window.sessionStorage.setItem('activePath', JSON.stringify(activePath))
    }
  }
}
</script>

<style lang="less" scoped>
.home-container{
  height: 100%;
}
.el-header{
  background-color: #4e5253;
  display: flex;
  justify-content: space-between;
  align-items: center;
  .header-box{
    display: flex;
    align-items: center;
    >img{
      width: 40px;
      height: 40px;
      border-radius: 50%;
    }
    >span{
      color: #ffffff;
      font-size: 20px;
      margin-left: 15px;
    }
  }
}
.el-aside{
  background-color: #313743;
  .el-menu{
    border-right: none;
  }
}
.el-main{
  background-color: #e9edf0;
}
.iconfont{
  margin-right: 10px;
}
.toggle-button{
  background-color: #495163;
  color: white;
  font-size: 10px;
  line-height: 24px;
  text-align: center;
  letter-spacing: 0.2rem;
  cursor: pointer;
}
</style>
